﻿#include "qtfontawesome_plugin.h"

#include "qfontawesome.h"

#include <qqml.h>
#include <QQmlEngine>
#include <QQmlContext>
#include <QFontDatabase>
#include <QDebug>
#include <QFile>

QtFontAwesomePlugin::QtFontAwesomePlugin(QObject *parent)
    : QQmlExtensionPlugin(parent)
{
#ifdef QT_STATIC
    Q_INIT_RESOURCE(fontresource);
    Q_INIT_RESOURCE(qmlqrc);
#endif
}

void QtFontAwesomePlugin::registerTypes(const char *uri)
{
    // @uri QtFontAwesome
    qmlRegisterType<QFontAwesome>(uri, 1, 0, "QFontAwesome");
}

void QtFontAwesomePlugin::initializeEngine(QQmlEngine *engine, const char *uri)
{
    int faweBr400 = QFontDatabase::addApplicationFont(":/otfs/Font Awesome 5 Brands-Regular-400.otf");
    int faweFr400 = QFontDatabase::addApplicationFont(":/otfs/Font Awesome 5 Free-Regular-400.otf");
    int faweFs900 = QFontDatabase::addApplicationFont(":/otfs/Font Awesome 5 Free-Solid-900.otf");

    QStringList fontName = QFontDatabase::applicationFontFamilies(faweBr400);
    QQmlContext *fontConext = engine->rootContext();
    fontConext->setContextProperty("faweBr400", fontName.at(0));
    qWarning()<<__FILE__<<__FUNCTION__<<__LINE__<<"==============faweBr400"<<QFile::exists("qrc:/otfs/Font Awesome 5 Brands-Regular-400.otf")<<faweBr400<<fontName;

    fontName = QFontDatabase::applicationFontFamilies(faweFr400);
    fontConext->setContextProperty("faweFr400", fontName.at(0));
    qWarning()<<__FILE__<<__FUNCTION__<<__LINE__<<"==============faweFr400"<<QFile::exists("qrc:/otfs/Font Awesome 5 Free-Regular-400.otf")<<faweFr400<<fontName;

    fontName = QFontDatabase::applicationFontFamilies(faweFs900);
    fontConext->setContextProperty("faweFs900", fontName.at(0));
    qWarning()<<__FILE__<<__FUNCTION__<<__LINE__<<"==============faweFs900"<<QFile::exists("qrc:/otfs/Font Awesome 5 Free-Solid-900.otf")<<faweFs900<<fontName;

}

